define(function(require,exports,module){
	exports.init = function(){
		
		common = require('common');
		require('ajaxproxy');
		$('.ajaxproxy').AjaxProxy({
			dataType : "json",
			method : "post",
			formId : "content-add-form",
			formCheckHandler : function(form_id) {
				return common.formCheckHandler(form_id, true);
			},
			callbackDelay : __global.jdialog.timer,
			timeInterval : __global.jdialog.timer,
			callBack : function (data) {   /* 执行ajax之后的回调函数 */
				common.ajaxCallback(data);
			}
		});


		//启用禁用
		$('#switch-status-verifyCode').bootstrapSwitch('onSwitchChange',function(e,d){
			var id = $(this).attr('data-id');
			$.post('do_edit',{id:id,data:{verifyCode: (d ?  '1' : '0')}},function(data){
				if ( data.code == "000" ) {
					JDialog.tip.work({type:"ok", content:data.message, lock:true, timer:500});
				} else {
					JDialog.tip.work({type:"error", content:data.message, timer:500});
				}
			},'json');

		});
		//启用禁用
		$('#switch-status-onePointLogin').bootstrapSwitch('onSwitchChange',function(e,d){
			var id = $(this).attr('data-id');
			$.post('do_edit',{id:id,data:{onePointLogin: (d ?  '1' : '0')}},function(data){
				if ( data.code == "000" ) {
					JDialog.tip.work({type:"ok", content:data.message, lock:true, timer:500});
				} else {
					JDialog.tip.work({type:"error", content:data.message, timer:500});
				}
			},'json');

		});

		function isIp(ip){
			var arr = ip.split('.');
			if(arr.length == 2){
				if(parseInt(arr[0]) >= 0  && parseInt(arr[0]) <= 255 && arr[1] == '*'){
					return true;     
				}
				return false;
			}
			if(!(parseInt(arr[0]) >= 0 && parseInt(arr[0]) <= 255 && parseInt(arr[1]) >= 0 && parseInt(arr[1]) <= 255)){
				return false;
			}
			if(arr.length == 3){
			
				if(arr[2] == '*') return true;
				return false;
			}
			if(!(parseInt(arr[2]) >= 0 && parseInt(arr[2]) <= 255)){
				return false;
			}
			if(arr.length == 4){
				unit = arr[3].split('/');
				if(unit.length == 2){
					if(parseInt(unit[0]) >= 0 && parseInt(unit[0]) <= 255 && parseInt(unit[1]) >= 1 && parseInt(unit[1]) <= 24){
						return true;
					}
					else{
						return false;
					}
				
				}
				if(unit.length > 2) return false;
				if(arr[3] == '*') return true;
				if(parseInt(arr[3]) >= 0 && parseInt(arr[3]) <= 255) return true;
			
			}
			return false;

				
		}
		$("#addIp").click(function(){
			var ip = $("#denyIp").val();
			var hasip = false;
			$('#iplist li').each(function(){ 
				if($(this).find('input').val() == ip){
					hasip = true;
				}
			});
			if(hasip) return ;
			if($.trim(ip)==''){
				alert('ip为空,不能添加');
				return ;
			}
			if(!isIp(ip)){
				alert('非法ip');
				return ;
			}
				
			ele = $('<li><input type="hidden" name="data[denyLoginIp][]" value="'+ip+'">'+ip+'<i class="glyphicon-remove glyphicon pull-right"></i></li>');
			$('#iplist').append(ele);
		});
		$("#addWord").click(function(){
			var word = $("#denyWord").val();
			var hasword = false;
			$('#wordlist li').each(function(){ 
				if($(this).find('input').val() == word){
					hasword = true;
				}
			});
			if(hasword) return ;
			if($.trim(word)==''){
				alert('违禁词为空,不能添加');
				return ;
			}
			ele = $('<li><input type="hidden" name="data[prohibitedWords][]" value="'+word+'">'+word+'<i class="glyphicon-remove glyphicon pull-right"></i></li>');
			$('#wordlist').append(ele);
		});
		$("#iplist,#wordlist").on('click','li i',function(){
			id = $(this).closest('li').find('input').val();
			$(this).closest('li').remove();
		});
	}		
});
